package tr.edu.yildiz.ce.app_manager.model;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Transient;

@Entity(name="APPLICATION")
public class Application extends BasicObject<Long> {
	
	@Id
	@SequenceGenerator(name = "application_generator", sequenceName = "SEQ_APPLICATION", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "application_generator")
    private Long applicationId;
	
	@Column(name = "APPLICATION_NAME", nullable = false, length = 8)
	private String applicationName;
	
	@Column(name = "APPLICATION_VERSION", nullable = false, length = 8)
	private String applicationVersion;
	
	@Lob
	@Column(name = "APP_XML", nullable = false,updatable=true)
	private Blob appXml;
	
	@ManyToMany(targetEntity=AppProxy.class,cascade=CascadeType.ALL,fetch=FetchType.EAGER)
	@JoinTable(name = "PROXY_APPLICATION",
	joinColumns = {@JoinColumn(name="APPLICATION_ID")},inverseJoinColumns={@JoinColumn(name="PROXY_ID")})
	private Set<AppProxy> proxies;
	
	@Column(name = "IS_AUTO_DEPLOY", nullable = false, length = 8)
	private Boolean autoDeploy;
	
	public Long getApplicationId() {
		return applicationId;
	}
	
	public void setApplicationId(Long applicationId) {
		this.applicationId = applicationId;
	}
	
	public Boolean isAutoDeploy() {
		return autoDeploy;
	}
	
	public void setAutoDeploy(Boolean autoDeploy) {
		this.autoDeploy = autoDeploy;
	}
	
	public String getApplicationName() {
		return applicationName;
	}
	
	public void setApplicationName(String applicationName) {
		this.applicationName = applicationName;
	}
	
	public byte[] getAppXmlAsByteArray() {
		InputStream is = null;
		try {
			is = appXml.getBinaryStream();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		int c = -1;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		try {
			while((c = is.read()) != -1){
				bout.write(c);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				is.close();
				bout.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return bout.toByteArray();
	}
	
	public void setAppXml(Blob appXml) {
		this.appXml = appXml;
	} 
	
	public Set<AppProxy> getProxies() {
		return proxies;
	}
	
	public void setProxies(Set<AppProxy> proxies) {
		this.proxies = proxies;
	}

	public String getApplicationVersion() {
		return applicationVersion;
	}
	
	public void setApplicationVersion(String applicationVersion) {
		this.applicationVersion = applicationVersion;
	}
	
	@Override
	public String toString() {
		return applicationName + "[" + applicationVersion + "]";
	}

	@Override
	public Long getId() {
		return applicationId;
	}
	
}
